package com.frogs.ucenter.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.component.cache.Cache;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.component.RsyncHelper;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.entity.UserIntegralBalance;
import com.frogs.ucenter.entity.UserMessageSmsVerifyCode;
import com.frogs.ucenter.service.AreaService;
import com.frogs.ucenter.service.OrderService;
import com.frogs.ucenter.service.SmsService;
import com.frogs.ucenter.service.TagService;
import com.frogs.ucenter.service.UploadService;
import com.frogs.ucenter.service.UserIntegralService;
import com.frogs.ucenter.service.UserRechargeService;
import com.frogs.ucenter.service.UserService;
import com.frogs.ucenter.service.UserShippingAddressService;
import com.frogs.ucenter.util.DataCacheKey;
import com.frogs.ucenter.util.SysStatusEnum;

@Controller
public class UserController extends AbsController {

	private static Logger log = LoggerFactory.getLogger(UserController.class);

	private static final String DOMAIN_SCRIPT = "<script>document.domain = 'frogshealth.com';</script>";
	
	private static final String LOGIN_VIEW = "user/login";
	private static final String HTML5_LOGIN_VIEW = "user/html5/login";
	private static final String HTML5_REGISTER_VIEW = "user/html5/register";
	private static final String FORGET_VIEW = "user/find_password";
	private static final String BASE_INFO_EDIT_VIEW = "user/baseInfoEdit";
	private static final String BASE_INFO_SHOW_VIEW = "user/index";
	private static final String RESET_PASSWORD = "user/reset";
	private static final String SECURITY_VIEW = "/user/security";
	
	private static final String USER_IS_BIND_PHONE = "/user/isBindph";
	private static final String USER_LOGIN = "/user/login";
	private static final String USER_REGISTER = "/user/register";
	private static final String LOGOUT = "/user/logout";
	private static final String LOGIN = "/user/loginService";
	private static final String REGISTER = "/user/registerService";
	private static final String SEND_REG_CODE = "/send/regCode";
	private static final String SEND_LOGIN_DYNAMICI_PASSWORD = "/send/loginPwCode";
	private static final String FORGET = "/user/forget";
	private static final String FORGET_FIND_BY_EMAIL = "/user/forget/findByEmail";
	private static final String RESET_PASSWORD_INDEX = "/user/resetPwBykey";
	private static final String RESET_PASSWORD_BY_EMAIL = "/user/resetPw";
	private static final String RESET_PASSWORD_BY_PHONE = "/user/resetPw/ByPhone";
	private static final String SEND_RESET_CODE = "/send/resetCode";
	private static final String SECURITY = "/user/security";
	private static final String USER_UPDATE_PASSWORD = "/user/updatePw";
	private static final String BASE_INFO_EDIT = "/user/baseInfo/edit";
	private static final String BASE_INFO_CHECK = "/user/baseInfo/check";
	private static final String BASE_INFO_UPDATE = "/user/baseInfo/update";
	private static final String BASE_INFO_SHOW = "/user/index";
	private static final String USER_EMAIL_BIND = "/user/email/bind";
	private static final String USER_EMAIL_UNBIND = "/user/email/unbind";
	private static final String USER_PHONE_BIND = "/user/phone/bind";
	private static final String USER_PHONE_UNBIND = "/user/phone/unbind";
	private static final String USER_IMG_UPLOAD = "/user/images/upload";
	private static final String USER_TAG_DELETE = "/user/tag/delete";
	private static final String USER_TAG_ADD = "/user/tag/add";
	private static final String USER_PROPERTY_DETAIL = "/user/property/detail";
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	@Resource
	private UserService userService;
	@Resource(name="areaService")
	private AreaService areaService;
	@Resource(name="tagService")
	private TagService tagService;
	@Resource(name="userShippingAddressService")
	private UserShippingAddressService addressService;
	@Resource
	private UploadService uploadService;
	@Resource
	private SmsService smsService;
	@Resource
	private OrderService orderService;
	@Resource
	private RsyncHelper rsyncHelper;
	@Resource(name="xmemcached")
	private Cache cache;
	@Resource
	private UserRechargeService userRechargeService;
    @Resource
    private UserIntegralService userIntegralService;
    
	@RequestMapping(USER_LOGIN)
	public String login(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		setRequestModelMap(request, model);
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		Integer platform = requestInfo.getIntParameter("platform", ApplicablePlatformEnum.WEB.getId());
		if (platform == ApplicablePlatformEnum.HTML5.getId()) {
			return HTML5_LOGIN_VIEW;
		}
		if (platform == ApplicablePlatformEnum.WECHAT.getId()) {
			return HTML5_LOGIN_VIEW;
		}
		return LOGIN_VIEW;
	}
	
	@RequestMapping(USER_REGISTER)
	public String register(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		setRequestModelMap(request, model);
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		Integer platform = requestInfo.getIntParameter("platform", ApplicablePlatformEnum.WEB.getId());
		if (platform == ApplicablePlatformEnum.HTML5.getId()) {
			return HTML5_REGISTER_VIEW;
		}
		if (platform == ApplicablePlatformEnum.WECHAT.getId()) {
			return HTML5_REGISTER_VIEW;
		}
		return "";
	}
	
	@RequestMapping(value=LOGIN, method=RequestMethod.POST)
	public Map<String, Object> loginService(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Map<String, Object> result = new HashMap<String, Object>();
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		String userName = requestInfo.getParameter("userName", "");
		String password = requestInfo.getParameter("password", "");
		String returnUrl = requestInfo.getParameter("ReturnUrl", "");
		int isphpw = requestInfo.getIntParameter("isphpw", 0); // 是否是动态密码登录 0：不是，1：是
		Integer isKeepPassword = requestInfo.getIntParameter("isKpw", 1); // 是否记住密码 0：不记住，1：记住
		String _wt_pk_cookie = requestInfo.getParameter("_wt_pk_cookie", "");
		result.put("ReturnUrl", returnUrl);
		try {
			if (StringUtil.isEmpty(userName)) {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.NAME_NULL.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.NAME_NULL.getCode());
				return result;
			}
			if (StringUtil.isEmpty(password)) {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_NULL.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_NULL.getCode());
				return result;
			}
			if (!StringUtil.isEmpty(password)) {
				if (password.length() < 6 || password.length() > 20) {
					result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return result;
				}
			}
			Map<String, Object> loginResult = userPermissionHelper.login(request, response, userName, password, isphpw, isKeepPassword, _wt_pk_cookie);
			if (loginResult != null) {
				if ((Integer) loginResult.get(ResultParamEnum.code.getParamName()) == SysStatusEnum.SUCCESS.getCode()) {
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
				} else {
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.LOGIN_ERR.getCode());
				}
				result.put(ResultParamEnum.tip.getParamName(), loginResult.get(ResultParamEnum.tip.getParamName()));
			} else {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.SUCCESS.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
			}
			return result;
		} catch (Exception e) {
			log.error("func[loginService] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 判断是否有绑定手机号
	 * 
	 * @param model
	 * @param username
	 */
	@RequestMapping(USER_IS_BIND_PHONE)
	public void isBindPhoneByUsername(ModelMap model, String username) {
		int code = SysStatusEnum.FAIL.getCode();
		if (StringUtil.isEmpty(username)) {
			model.put(ResultParamEnum.code.getParamName(), code);
			model.put(ResultParamEnum.tip.getParamName(), "账号名不能为空");
			return ;
		}
		boolean flag;
		try {
			flag = userService.isBindPhoneByUsername(username);
			if (flag) {
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
				model.put(ResultParamEnum.tip.getParamName(), "该账户存在绑定手机号码");
			} else {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "该账户手机号码不存在");
			}
		} catch (Exception e) {
			log.error("func[isBindPhoneByUsername] username[{}] exception[{} - {}] desc[fail]",//
					new Object[] { username, e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
	
	/**
	 * 登出
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(LOGOUT)
	public String logout(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		try {
			userPermissionHelper.logout(request, response);
		} catch (Exception e) {
			log.error("func[logout] exception[{} - {}] desc[fail]",//
					new Object[] { e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
		return null;
	}
	
	@RequestMapping(value=REGISTER, method=RequestMethod.POST)
	public Map<String, Object> registerService(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Map<String, Object> result = new HashMap<String, Object>();
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		String userName = requestInfo.getParameter("userName", "");
		String nickName = requestInfo.getParameter("nickName", "");
		String password = requestInfo.getParameter("password", "");
		String confirmPassword = requestInfo.getParameter("confirmPassword", "");
		String verifyCode = requestInfo.getParameter("verifyCode", "");
		String imgCode = requestInfo.getParameter("imgCode", "");
		String returnUrl = requestInfo.getParameter("ReturnUrl", "");
		String _wt_pk_cookie = requestInfo.getParameter("_wt_pk_cookie", "");
		Integer platform = requestInfo.getIntParameter("platform", ApplicablePlatformEnum.WEB.getId());
		result.put("ReturnUrl", returnUrl);
		try {
			if (StringUtil.isEmpty(userName)) {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.NAME_NULL.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.NAME_NULL.getCode());
				return result;
			}
			if (StringUtil.isMobilePhone(userName)) {
				if (StringUtil.isEmpty(verifyCode)) {
					result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PHONE_CODE_ERR.getMsg());
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PHONE_CODE_ERR.getCode());
					return result;
				} else {
					if(!smsService.checkVerifyCode(userName, verifyCode, UserMessageSmsVerifyCode.SendOperationEnum.REGISTER.getValue())) {
						result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.VALIDATE_CODE_ERR.getMsg());
						result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.VALIDATE_CODE_ERR.getCode());
						return result;
					}
				}
			} else {
				String msg = checkValidateCode(imgCode, request, response);
				if (!"success".equals(msg)) {
					result.put(ResultParamEnum.tip.getParamName(), msg);
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.VALIDATE_CODE_ERR.getCode());
					return result;
				}
			}
			Map<String, Object> obj = userPermissionHelper.register(request, response, userName, nickName, password, confirmPassword, _wt_pk_cookie, platform);
			if (obj != null) {
				if ((Integer) obj.get(ResultParamEnum.code.getParamName()) == SysStatusEnum.SUCCESS.getCode()) {
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
				} else {
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.REGISTER_ERR.getCode());
				}
				result.put(ResultParamEnum.tip.getParamName(), obj.get(ResultParamEnum.tip.getParamName()));
			} else {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.REGISTER_ERR.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.REGISTER_ERR.getCode());
			}
		} catch (Exception e) {
			log.error("func[registerService] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 发送注册时手机验证码
	 * 
	 * @param model
	 * @param phone
	 * @return
	 */
	@RequestMapping(SEND_REG_CODE)
	public JSONObject sendRegisterVerifyCode(HttpServletRequest request, HttpServletResponse response){
		JSONObject model = new JSONObject();
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		String phone = requestInfo.getParameter("phone", "");
		String imgCode = requestInfo.getParameter("imgCode", "");
		try {
			String msg = checkValidateCode(imgCode, request, response);
			if (!"success".equals(msg)) {
				model.put(ResultParamEnum.tip.getParamName(), msg);
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.VALIDATE_CODE_ERR.getCode());
				return model;
			}
			int code = SysStatusEnum.FAIL.getCode();
			if (!StringUtil.isMobilePhone(phone)) {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "手机号码有误");
				return model;
			}
			if (cache.get(DataCacheKey.registerCodeAgingMapkey+phone) != null) {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "请两分钟后再发");
				return model;
			}
			boolean flag;
			
			flag = userService.verifyPhone(phone);
			if (flag) {
				if(smsService.sendVerifyByphone(phone, UserMessageSmsVerifyCode.SendOperationEnum.REGISTER.getValue(), 60*5)) {
					model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
					model.put(ResultParamEnum.tip.getParamName(), "验证码发送成功");
					// 设置2分钟的限制
					cache.put(DataCacheKey.registerCodeAgingMapkey+phone, model.toString(), 120);
				} else {
					model.put(ResultParamEnum.code.getParamName(), code);
					model.put(ResultParamEnum.tip.getParamName(), "验证码发送失败");
				}
			} else {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "手机号码已存在");
			}
		} catch (Exception e) {
			log.error("func[sendRegisterVerifyCode] phone[{}] exception[{} - {}] desc[fail]",//
					new Object[] { phone, e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return model;
	}
	
	/**
	 * 发送登录时手机动态密码
	 * 
	 * @param model
	 * @param phone
	 * @return
	 */
	@RequestMapping(SEND_LOGIN_DYNAMICI_PASSWORD)
	public JSONObject sendLoginDynamicPassword(HttpServletRequest request, HttpServletResponse response){
		JSONObject model = new JSONObject();
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		String userName = requestInfo.getParameter("userName", "");
		String imgCode = requestInfo.getParameter("imgCode", "");
		try {
			String msg = checkValidateCode(imgCode, request, response);
			if (!"success".equals(msg)) {
				model.put(ResultParamEnum.tip.getParamName(), msg);
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.VALIDATE_CODE_ERR.getCode());
				return model;
			}
			int code = SysStatusEnum.FAIL.getCode();
			if (StringUtil.isEmpty(userName)) {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "用户名不能为空");
				return model;
			}
			if (cache.get(DataCacheKey.loginDynamicPasswordAgingMapkey + userName) != null) {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "请两分钟后再发");
				return model;
			}
			boolean flag;
			
			String ip = requestInfo.getUserIp();
			flag = userService.sendDynamicPaswordByPhone(userName, ip);
			if (flag) {
					model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
					model.put(ResultParamEnum.tip.getParamName(), "动态密码发送成功");
					// 设置2分钟的限制
					cache.put(DataCacheKey.loginDynamicPasswordAgingMapkey+userName, model.toString(), 120);
				} else {
					model.put(ResultParamEnum.code.getParamName(), code);
					model.put(ResultParamEnum.tip.getParamName(), "动态密码发送失败");
				}
		} catch (Exception e) {
			log.error("func[sendLoginDynamicPassword] username[{}] exception[{} - {}] desc[fail]",//
					new Object[] { userName, e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return model;
	}
	
	/**
	 * 账号安全
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(SECURITY)
	public String security(HttpServletRequest request, ModelMap model) {
		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
		if (user == null) {
			return ERROR_VIEW;
		}
		model.put("user", user);
		return SECURITY_VIEW;
	}
	
	/**
	 * 更新密码
	 */
	@RequestMapping(value=USER_UPDATE_PASSWORD, method=RequestMethod.POST)
	public Map<String, Object> updatePw(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Map<String, Object> result = new HashMap<String, Object>();
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		try {
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			if (user == null) {
				result.put(ResultParamEnum.tip.getParamName(), "用户未登录");
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return result;
			}
			String oldPassword = requestInfo.getParameter("oldPassword", "");
			String newPassword = requestInfo.getParameter("newPassword", "");
			String newConfirmPassword = requestInfo.getParameter("newConfirmPassword", "");
			if (StringUtil.isEmpty(oldPassword) || StringUtil.isEmpty(newPassword) || StringUtil.isEmpty(newConfirmPassword)) {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_NULL.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_NULL.getCode());
				return result;
			}
			if (!StringUtil.isEmpty(oldPassword)) {
				if (oldPassword.length() < 6 || oldPassword.length() > 20) {
					result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return result;
				}
			}
			if (!StringUtil.isEmpty(newPassword)) {
				if (newPassword.length() < 6 || newPassword.length() > 20) {
					result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return result;
				}
			}
			if (!StringUtil.isEmpty(newConfirmPassword)) {
				if (newConfirmPassword.length() < 6 || newConfirmPassword.length() > 20) {
					result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return result;
				}
			}
			if (!newPassword.equals(newConfirmPassword)) {
				result.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR_SAME.getMsg());
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR_SAME.getCode());
				return result;
			}
			
			String ip = requestInfo.getUserIp(request);
			JSONObject resultMap = userService.updatePasswd(user.getId(), oldPassword, newPassword, newConfirmPassword, ip);
			if (resultMap != null) {
				if (ApiResultEnum.SUCCESS.getId() == resultMap.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName())) {
					result.put(ResultParamEnum.tip.getParamName(), "密码更新成功");
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
					userPermissionHelper.login(request, response, user.getAccountName(), newPassword, 0, 1, "");
				} else {
					result.put(ResultParamEnum.tip.getParamName(), resultMap.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
					result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				}
			} else {
				result.put(ResultParamEnum.tip.getParamName(), "密码更新失败");
				result.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
			}
		} catch (Exception e) {
			log.error("func[updatePw] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 忘记密码首页
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value=FORGET)
	public String forget(HttpServletRequest request, ModelMap model) {
		setRequestModelMap(request, model);
		return FORGET_VIEW;
	}
	
	/**
	 * 重置密码首页
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(RESET_PASSWORD_INDEX)
	public String resetPwIndex(HttpServletRequest request, ModelMap model) {
		setRequestModelMap(request, model);
		String ui = (String) model.get("ui");
		String key = (String) model.get("k");
		if (StringUtil.isEmpty(ui) || StringUtil.isEmpty(key)) {
			return FORGET_VIEW;
		}
		return RESET_PASSWORD;
	}
	
	/**
	 * 发送重置密码邮件
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value=FORGET_FIND_BY_EMAIL, method=RequestMethod.POST)
	public Map<String, Object> forgetByEmail(HttpServletRequest request, ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			String email = requestInfo.getParameter("email", "");
			if (!StringUtil.isEmpty(cache.get(DataCacheKey.sendEmailVerifyMapkey + email))) {
				model.put(ResultParamEnum.tip.getParamName(), "请5分钟后再重发");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return model;
			}
			JSONObject result = userService.findPwd("", email, "", requestInfo.getUserIp());
			if (result == null) {
				model.put(ResultParamEnum.tip.getParamName(), "用户不存在，或未绑定邮箱");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return model;
			}
			Integer userId = result.getInteger("userId");
			boolean flag = userService.sendEmailPwd(userId, email);
			if (!flag) {
				model.put(ResultParamEnum.tip.getParamName(), "发送邮件失败，请重试");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
			} else {
				cache.put(DataCacheKey.sendEmailVerifyMapkey + email, StringUtil.md5(email), 60 * 5);
				model.put(ResultParamEnum.tip.getParamName(), "发送成功");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
			}
		} catch (Exception e) {
			log.error("func[forgetByEmail] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return model;
	}
	
	/**
	 * 邮箱回来修改密码提交
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value=RESET_PASSWORD_BY_EMAIL, method=RequestMethod.POST)
	public Map<String, Object> resetPasswordByEmail(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			String password = requestInfo.getParameter("password", "");
			String confirmPassword = requestInfo.getParameter("confirmPassword", "");
			String ui = requestInfo.getParameter("ui", "");
			String key = requestInfo.getParameter("k", "");
			if (StringUtil.isEmpty(password) || StringUtil.isEmpty(confirmPassword)) {
				model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_NULL.getMsg());
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_NULL.getCode());
				return model;
			}
			if (!StringUtil.isEmpty(password)) {
				if (password.length() < 6 || password.length() > 20) {
					model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return model;
				}
			}
			if (!StringUtil.isEmpty(confirmPassword)) {
				if (confirmPassword.length() < 6 || confirmPassword.length() > 20) {
					model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return model;
				}
			}
			if (!password.equals(confirmPassword)) {
				model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR_SAME.getMsg());
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR_SAME.getCode());
				return model;
			}
			if (StringUtil.isEmpty(ui) || StringUtil.isEmpty(key)) {
				model.put(ResultParamEnum.tip.getParamName(), "修改失败");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return model;
			}
			String userId = StringUtil.base64Decode(ui);
			boolean flag = userService.resetPasswd(userId, key, password, confirmPassword, requestInfo.getUserIp());
			if (!flag) {
				model.put(ResultParamEnum.tip.getParamName(), "重置密码失败，请重试");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
			} else {
				model.put(ResultParamEnum.tip.getParamName(), "密码修改成功");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
				userPermissionHelper.logout(request, response);
			}
		} catch (Exception e) {
			log.error("func[resetPasswordByEmail] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return model;
	}
	
	/**
	 * 发送修改密码的手机验证码
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value=SEND_RESET_CODE)
	public Map<String, Object> sendResetcodeByPhone(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			String phone = requestInfo.getParameter("phone", "");
			String imgCode = requestInfo.getParameter("imgCode", "");
			String msg = checkValidateCode(imgCode, request, response);
			if (!"success".equals(msg)) {
				model.put(ResultParamEnum.tip.getParamName(), msg);
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.VALIDATE_CODE_ERR.getCode());
				return model;
			}
			JSONObject result = userService.findPwd("", "", phone, requestInfo.getUserIp());
			if (result == null) {
				model.put(ResultParamEnum.tip.getParamName(), "用户不存在，或未绑定手机号码");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return model;
			}
			model.put("userId", result.getInteger("userId"));
			int code = SysStatusEnum.FAIL.getCode();
			if (!StringUtil.isMobilePhone(phone)) {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "手机号码有误");
				return model;
			}
			if (cache.get(DataCacheKey.resetPwCodeAgingMapkey+phone) != null) {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "请5分钟后再发");
				return model;
			}
			if(smsService.sendVerifyByphone(phone, UserMessageSmsVerifyCode.SendOperationEnum.FIND_PASSWORD.getValue(), 60*5)) {
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
				model.put(ResultParamEnum.tip.getParamName(), "验证码发送成功");
				// 设置5分钟的限制
				cache.put(DataCacheKey.resetPwCodeAgingMapkey+phone, model.toString(), 60*5);
			} else {
				model.put(ResultParamEnum.code.getParamName(), code);
				model.put(ResultParamEnum.tip.getParamName(), "验证码发送失败");
			}
		} catch (Exception e) {
			log.error("func[sendResetcodeByPhone] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return model;
	}
	
	/**
	 * 根据手机号码重置密码
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value=RESET_PASSWORD_BY_PHONE,  method=RequestMethod.POST)
	public Map<String, Object> resetPasswordByPhone(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			String userId = requestInfo.getParameter("userId", "");
			String phone = requestInfo.getParameter("phone", "");
			String key = requestInfo.getParameter("key", "");
			String password = requestInfo.getParameter("password", "");
			String confirmPassword = requestInfo.getParameter("confirmPassword", "");
			if (StringUtil.isEmpty(userId) || StringUtil.isEmpty(phone) || StringUtil.isEmpty(key)) {
				model.put(ResultParamEnum.tip.getParamName(), "信息丢失");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return model;
			}
			if (StringUtil.isEmpty(password) || StringUtil.isEmpty(confirmPassword)) {
				model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_NULL.getMsg());
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_NULL.getCode());
				return model;
			}
			if (!StringUtil.isEmpty(password)) {
				if (password.length() < 6 || password.length() > 20) {
					model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return model;
				}
			}
			if (!StringUtil.isEmpty(confirmPassword)) {
				if (confirmPassword.length() < 6 || confirmPassword.length() > 20) {
					model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR.getMsg());
					model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR.getCode());
					return model;
				}
			}
			if (!password.equals(confirmPassword)) {
				model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.PASSWORD_ERR_SAME.getMsg());
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.PASSWORD_ERR_SAME.getCode());
				return model;
			}
			boolean flag = userService.resetPasswdByPhone(userId, phone, key, password, confirmPassword, requestInfo.getUserIp());
			if (flag) {
				model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.SUCCESS.getMsg());
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.SUCCESS.getCode());
				cache.remove(DataCacheKey.resetPwCodeAgingMapkey+phone);
				userPermissionHelper.logout(request, response);
			} else {
				model.put(ResultParamEnum.tip.getParamName(), "重置密码失败，请重试");
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
			}
		} catch (Exception e) {
			log.error("func[resetPasswordByPhone] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return model;
	}
	
	@RequestMapping(BASE_INFO_EDIT)
	public String editBaseInfo(HttpServletRequest request) {
		try {
			return BASE_INFO_EDIT_VIEW;
		} catch (Exception e) {
			log.error("func[editBaseInfo] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return ERROR_VIEW;
	}
	
	@RequestMapping(BASE_INFO_UPDATE)
	public JSONObject updateBaseInfo(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
		JSONObject resultJson = new JSONObject();
		JSONObject responseJson = new JSONObject();
		resultJson.put("response", responseJson);
		responseJson.put("code", 0);
		responseJson.put("tip", "操作失败");
		
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			
			
			Map<String,Object> baseInfo = new HashMap<String,Object>();
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			String phone = null;
			
			int uid=user.getId();
			if(uid <= 0) {
				responseJson.put(ResultParamEnum.tip.getParamName(), ApiResultEnum.FAIL.getName());
				responseJson.put(ResultParamEnum.code.getParamName(), ApiResultEnum.FAIL.getId());
				return resultJson;
			}
			baseInfo.put(ApiUrlParamEnum.userId.getParamName(), uid);
			
			
			String orderPhone = requestInfo.getParameter(ApiUrlParamEnum.orderPhone.getParamName());
			if(!StringUtil.isEmpty(orderPhone)) {
				baseInfo.put(ApiUrlParamEnum.orderPhone.getParamName(), orderPhone);
				JSONArray json = JSONObject.parseArray(orderPhone);
				if(json.size() > 0) {
					phone = json.getJSONObject(0).getString("phone");
				}
			}
			
			String headImageUrl = requestInfo.getParameter(ApiUrlParamEnum.headImageUrl.getParamName());
			if(!StringUtil.isEmpty(headImageUrl)) {
				baseInfo.put(ApiUrlParamEnum.headImageUrl.getParamName(), headImageUrl);
			}
			
			int age = requestInfo.getIntParameter(ApiUrlParamEnum.age.getParamName(), -1);
			if(age > 0) {
				baseInfo.put(ApiUrlParamEnum.age.getParamName(), age);
			}
			
			int sex = requestInfo.getIntParameter(ApiUrlParamEnum.sex.getParamName(), -1);
			if(sex > 0) {
				baseInfo.put(ApiUrlParamEnum.sex.getParamName(), sex);
			}
			
			String nickName = requestInfo.getParameter(ApiUrlParamEnum.nickName.getParamName(), null);
			if(!StringUtil.isEmpty(nickName)) {
				baseInfo.put(ApiUrlParamEnum.nickName.getParamName(), nickName);
			}
			
			String realName = requestInfo.getParameter(ApiUrlParamEnum.realName.getParamName(), "");
			if(!StringUtil.isEmpty(realName)) {
				baseInfo.put(ApiUrlParamEnum.realName.getParamName(), realName);
			}
			
			String birthday = requestInfo.getParameter(ApiUrlParamEnum.birthday.getParamName());
			if(!StringUtil.isEmpty(birthday)) {
				baseInfo.put(ApiUrlParamEnum.birthday.getParamName(), birthday);
			}
			
			String province = requestInfo.getParameter("province");
			String city = requestInfo.getParameter("city");
			String district = requestInfo.getParameter("district");
			int addrId = requestInfo.getIntParameter("addrId",-1);
			String areaId = requestInfo.getParameter("areaId");
			
			if(!StringUtil.isEmpty(province) && !StringUtil.isEmpty(city) && !StringUtil.isEmpty(district)  && !StringUtil.isEmpty(areaId)) {
				addressService.mergeUserDefaultAddress(user.getId(), addrId, areaId, province, city, district, phone);
			}
			
			JSONObject updateResult = userService.updateUserBaseInfo(baseInfo);
			//接口返回操作失败
			if(ApiResultEnum.SUCCESS.getId() != updateResult.getJSONObject("response").getIntValue("code")) {
				responseJson.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.FAIL.getMsg());
				responseJson.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
				return resultJson;
			}
			//设置cookie
			user = userPermissionHelper.getUser(request);
			userPermissionHelper.setUserCookie(request, response, user);
			return updateResult;
		} catch (Exception e) {
			log.error("func[updateBaseInfo] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return resultJson;
	}
	
	@RequestMapping(BASE_INFO_SHOW)
	public String showBaseInfo(HttpServletRequest request,ModelMap model) {
		try {
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			
			if (null == user) {
				model.put(ResultParamEnum.tip.getParamName(), SysStatusEnum.NOT_LOGIN_ERR.getMsg());
				model.put(ResultParamEnum.code.getParamName(), SysStatusEnum.NOT_LOGIN_ERR.getCode());
				return ERROR_VIEW;
			}
			
			int uid = user.getId();
			Map<String,Object> userInfo = userService.getUserBaseInfoForMap(uid);
			if(null != userInfo && !userInfo.isEmpty()) {
				model.put("userInfo", userInfo);
			}
			model.put("allTag", tagService.getTagListBySexaAge((Integer)userInfo.get("sex"), (Integer)userInfo.get("age")));
			model.put("userAccount", user);
			
			return BASE_INFO_SHOW_VIEW;
		} catch (Exception e) {
			log.error("func[showBaseInfo] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
	}
	
	/**
	 * 绑定邮箱
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_EMAIL_BIND)
	public JSONObject bindEmail(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			JSONObject response = new JSONObject();
			JSONObject result = new JSONObject();
			
			String email = requestInfo.getParameter("email",null);
			
			if(!StringUtil.isEmail(email) || StringUtil.isEmpty(email)) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "邮箱格式错误!");
				result.put("response", response);
				return result;
			}
			
			JSONObject json = userService.bindEmail(user.getId(), email, requestInfo.getUserIp());
			if(json == null) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "绑定邮箱失败!");
				result.put("response", response);
				return result;
			}
			
			return json;
		} catch (Exception e) {
			log.error("func[bindEmail] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 解绑邮箱
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_EMAIL_UNBIND)
	public JSONObject unBindEmail(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			JSONObject result = new JSONObject();
			JSONObject response = new JSONObject();
			
			String email = requestInfo.getParameter("email",null);
			
			if(!StringUtil.isEmail(email) || StringUtil.isEmpty(email)) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "邮箱格式错误!");
				result.put("response", response);
				return result;
			}
			
			JSONObject json = userService.unbindEmail(user.getId(), email);
			if(json == null) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "解绑邮箱失败!");
				result.put("response", response);
				return result;
			}
			
			return json;
		} catch (Exception e) {
			log.error("func[unBindEmail] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 绑定账户手机
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_PHONE_BIND)
	public JSONObject bindPhone(HttpServletRequest request) {
		JSONObject result = new JSONObject();
		JSONObject response = new JSONObject();
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			String phone = requestInfo.getParameter("phone",null);
			String code = requestInfo.getParameter("code",null);
			
			if(!StringUtil.isMobilePhone(phone) || StringUtil.isEmpty(phone)) {
				throw new Exception("手机格式错误!");
			}
			
			if(!smsService.checkVerifyCode(phone, code, UserMessageSmsVerifyCode.SendOperationEnum.BIND.getValue())) {
				throw new Exception("验证码校验错误!");
			}
			
			JSONObject json = userService.bindPhone(user.getId(), phone, requestInfo.getUserIp());
			if (null != json &&
					ApiResultEnum.SUCCESS.getId() == json.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName())) {
				
				JSONObject resultJson = orderService.bindingOrderPhone(phone, user.getId());
				Map<String,Object> userInfo = null;
				List<Map<String,Object>> phones = new ArrayList<Map<String,Object>>();
				userInfo = userService.getUserBaseInfoForMap(user.getId());
				if(null != userInfo) {
					//保存订单号码
					phones = (List<Map<String, Object>>) userInfo.get("orderPhones");
					if(null == phones) {
						phones = new ArrayList<Map<String,Object>>();
					}
					Map<String,Object> phoneMap = new HashMap<String,Object>();
					phoneMap.put("phone", phone);
					phoneMap.put("isVerify", 1);
					phoneMap.put("isImport", 0);
					if(phones.contains(phoneMap)) {
						phones.remove(phoneMap);
					}
					phones.add(0, phoneMap);
					userInfo.put(ApiUrlParamEnum.userId.getParamName(), user.getId());
					userInfo.put(ApiUrlParamEnum.orderPhone.getParamName(), JSONObject.toJSONString(phones));
					userService.updateUserBaseInfo(userInfo);
					//同步用户地址
					if(ApiResultEnum.SUCCESS.getId() == resultJson.getJSONObject("response").getIntValue("code")) {
						rsyncHelper.rsyncPhoneAddress(user.getId(), phone);
					}
				}
				response.put("ophone", JSONObject.toJSONString(phones));
				response.put("code", ApiResultEnum.SUCCESS.getId());
				response.put("tip", "成功绑定手机!");
				result.put("response", response);
				return result;
			} else {
				String exceptionMessage = "手机号码绑定失败!";
				if(null != json) {
					exceptionMessage = json.getJSONObject("response").getString("tip");
				}
				throw new Exception(exceptionMessage);
			}
			
		} catch (Exception e) {
			log.error("func[bindPhone] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			response.put("code", ApiResultEnum.FAIL.getId());
			response.put("tip", e.getMessage());
			result.put("response", response);
			return result;
		}
	}
	
	/**
	 * 用户头像上传
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_IMG_UPLOAD)
	public String uploadImage(HttpServletRequest request, HttpServletResponse response) {
		JSONObject result = new JSONObject(); 
		JSONObject responseJson = new JSONObject();
		
		try {
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			response.setCharacterEncoding("utf-8");
			
			JSONObject json = uploadService.uploadImage(request, user);
			if(json == null) {
				responseJson.put("code", ApiResultEnum.FAIL.getId());
				responseJson.put("tip", "上传失败");
				result.put("response", responseJson);
				response.getWriter().print( DOMAIN_SCRIPT + result.toJSONString());
				return null;
			} else {
				if(json.getJSONObject("response").getIntValue("code") == ApiResultEnum.SUCCESS.getId()) {
					JSONArray jsr = json.getJSONObject("response").getJSONObject("result").getJSONArray("imgPaths"); 
					String headImg = jsr.getJSONObject(0).getString("original");
					Map<String,Object> userBase = new HashMap<String,Object>();
					userBase.put(ApiUrlParamEnum.userId.getParamName(), user.getId());
					userBase.put(ApiUrlParamEnum.headImageUrl.getParamName(), headImg);
					userService.updateUserBaseInfo(userBase);
				}
			}
			
			response.getWriter().print( DOMAIN_SCRIPT + json.toJSONString());
			return null;
		} catch (Exception e) {
			log.error("func[uploadImage] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			responseJson.put("code", ApiResultEnum.FAIL.getId());
			responseJson.put("tip", "上传失败");
			result.put("response", responseJson);
			try {
				response.getWriter().print( DOMAIN_SCRIPT + result.toJSONString());
			} catch(Exception e1) {
				log.error("func[uploadImage] parameterMap[{}] exception[{} - {}] desc[fail]",//
						new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			}
		}
		return null;
	}
	
	/**
	 * 解绑账户手机
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_PHONE_UNBIND)
	public JSONObject unbindPhone(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			JSONObject result = new JSONObject();
			JSONObject response = new JSONObject();
			
			String phone = requestInfo.getParameter("phone",null);
			String code = requestInfo.getParameter("code",null);
			
			if(!StringUtil.isMobilePhone(phone) || StringUtil.isEmpty(phone)) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "手机格式错误!");
				result.put("response", response);
				return result;
			}
			
			if(!smsService.checkVerifyCode(phone, code, UserMessageSmsVerifyCode.SendOperationEnum.BIND.getValue())) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "验证码校验错误!");
				result.put("response", response);
				return result;
			}
			
			JSONObject json = userService.unbindPhone(user.getId(), phone);
			if(json == null) {
				response.put("code", ApiResultEnum.FAIL.getId());
				response.put("tip", "解绑手机失败!");
				result.put("response", response);
				return result;
			}
			
			return json;
		} catch (Exception e) {
			log.error("func[unBindEmail] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 删除用户标签
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_TAG_DELETE)
	public JSONObject deleteUserTag(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			int tagId = requestInfo.getIntParameter(ApiUrlParamEnum.tagId.getParamName(), -1);
			if(tagId <= 0) {
				throw new Exception("错误的标签ID,tagId = " + tagId);
			}
			
			JSONObject json = tagService.deleteUserTag(user.getId(), tagId);
			
			return json;
		} catch (Exception e) {
			log.error("func[deleteUserTag] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 添加用户标签
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_TAG_ADD)
	public JSONObject addUserTag(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			int tagId = requestInfo.getIntParameter(ApiUrlParamEnum.tagId.getParamName(), -1);
			if(tagId <= 0) {
				throw new Exception("错误的标签ID,tagId = " + tagId);
			}
			
			JSONObject json = tagService.relateUserTag(user.getId(), tagId);
			
			return json;
		} catch (Exception e) {
			log.error("func[addUserTag] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	@RequestMapping(BASE_INFO_CHECK)
	public JSONObject checkUserBaseInfo(HttpServletRequest request) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			JSONObject json = userService.checkUserBaseInfo(user.getId());
			return json;
		} catch (Exception e) {
			log.error("func[checkUserBaseInfo] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	@RequestMapping(USER_PROPERTY_DETAIL)
	public JSONObject getUserPropertyDetail(HttpServletRequest request) {
		JSONObject json = null;
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			String userBalance = userRechargeService.getUserBalanceInfo(user.getId(), user.getVerify());//余额
			UserIntegralBalance integralBalance = userIntegralService.getUserIntegralInfo(user.getId());
			String iBalance = "";//积分
			Map<String, Object> map = new HashMap<String, Object>();
			if(StringUtil.isEmpty(userBalance)){
				userBalance = "0";
			}
			if(StringUtil.isEmpty(integralBalance)){
				iBalance = "0";
			}else{
				iBalance = integralBalance.getIntegralUsable().toString();
			}
			map.put("userBalance", userBalance);
			map.put("integralBalance", iBalance);
			json = new JSONObject(map);
			return json;
		} catch (Exception e) {
			log.error("func[checkUserBaseInfo] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return json;
	}
	
}
